<?php


namespace Catmes\HttpClient;


class Request
{
    const METHOD_POST = 'POST';
    const METHOD_GET = 'GET';

    const HEADER_KEY_CONTENT_TYPE = 'Content-Type';
    const HEADER_KEY_CONTENT_LENGTH = 'Content-Length';
    const HEADER_KEY_ACCEPT = 'accept';
    const HEADER_KEY_ACCEPT_LANG = 'accent-language';
    const HEADER_KEY_USER_AGENT = 'user-agent';

    const ACCEPT_JSON = 'application/json; charset=utf-8';
    const ACCEPT_HTML = 'text/html,application/xhtml+xml,application/xml';
    const ACCEPT_LANG_CN = 'zh-CN,zh;q=0.9';
    const USER_AGENT_WIN10_CHROME = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36';

    const CONTENT_TYPE_X_WWW_FORM_URLENCODED = 'application/x-www-form-urlencoded';
    const CONTENT_TYPE_JSON = 'application/json';
    const CONTENT_TYPE_FORM_DATA = 'multipart/form-data';
    const CONTENT_TYPE_XML = 'text/xml';


    protected $url;
    protected $method;
    protected $headers=[];
    protected $postData;
    protected $postString;
    protected $cookieFile;
    public $sslVerify = false;
    public $cookieSave = false;
    public $cookieRequest = false;
    public $followLocation = false;

    public function __construct($url, $method=self::METHOD_GET){
        $this->url = $url;
        $this->headers = self::getDefaultHeaders();
        $this->method = strtoupper($method);
    }

    protected static function getDefaultHeaders()
    {
        $userAgent = self::USER_AGENT_WIN10_CHROME;
        $userAgentFile = __DIR__ . "/user_agent.txt";
        if(is_file($userAgentFile)){
            $userAgent = file_get_contents($userAgentFile);
        }
        return [
            self::HEADER_KEY_ACCEPT_LANG => self::ACCEPT_LANG_CN,
            self::HEADER_KEY_USER_AGENT => $userAgent,
        ];
    }

    protected function setPostInfo($data, $contentType){
        $this->postData = $data;
        $this->setHeader(self::HEADER_KEY_CONTENT_TYPE, $contentType);
        return $this;
    }

    public function enableSslVerify(){
        return $this->sslVerify = true;
    }
    public function disableSslVerify(){
        return $this->sslVerify = false;
    }

    protected function checkCookieFile($filepath){
        if($filepath!=''){
            $this->cookieFile = $filepath;
        }
        if(!$this->cookieFile){
            throw new \Exception('cookie文件未设置');
        }
    }

    public function cookieSave($filepath=''){
        $this->checkCookieFile($filepath);
        $this->cookieSave = true;
        return $this;
    }

    public function getCookieFile(){
        return $this->cookieFile;
    }

    public function cookieRequest($filepath=''){
        $this->checkCookieFile($filepath);
//        if(!is_file($this->cookieFile)){
//            throw new \Exception('cookie文件不存在');
//        }
        $this->cookieRequest = true;
        return $this;
    }

    public function setPostData(array $data){
        $this->postData = $data;
        return $this;
    }

    public function setPostJson($data){
        $this->postString = json_encode($data);
        $this->setHeader(self::HEADER_KEY_CONTENT_LENGTH, strlen($this->postString));
        $this->setPostInfo($data, self::CONTENT_TYPE_JSON);
        return $this;
    }

    public function setPostUrlEncoded($data){
        $this->postString = http_build_query($data);
        $this->setHeader(self::HEADER_KEY_CONTENT_LENGTH, strlen($this->postString));
        $this->setPostInfo($data, self::CONTENT_TYPE_X_WWW_FORM_URLENCODED);
        return $this;
    }

    public function setPostXml($data){
        $this->setPostInfo($data, self::CONTENT_TYPE_XML);
        return $this;
    }

    public function getPostDataForPhpCurl(){
        $contentType = $this->headers[self::HEADER_KEY_CONTENT_TYPE] ?? null;
        $strTypes = [self::CONTENT_TYPE_X_WWW_FORM_URLENCODED, self::CONTENT_TYPE_JSON];
        if(in_array($contentType, $strTypes)){
            return $this->postString;
        }
        return $this->postData;
    }

    public function isPost(){
        if($this->method == self::METHOD_POST){
            return true;
        }
        return false;
    }

    public function getMethod(){
        return $this->method;
    }

    public function getUrl(){
        return $this->url;
    }

    public function getHeaders(){
        $headers = [];
        foreach ($this->headers as $key => $value){
            $headers[] = "{$key}: {$value}";
        }
        return $headers;
    }

//    public function addHeaders($headers){
//        $this->headers = array_merge($this->headers,$headers);
//        return $this;
//    }

    public function setHeader($key, $value){
        $this->headers[$key] = $value;
        return $this;
    }


}